package com.tjec.project.system.service.impl;

import com.tjec.common.constant.AttachmentConstants;
import com.tjec.common.utils.DateUtils;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.domain.BmsAttachmentHistory;
import com.tjec.project.system.domain.FileInfo;
import com.tjec.project.system.mapper.BmsAttachmentMapper;
import com.tjec.project.system.service.FileInfoService;
import com.tjec.project.system.service.IBmsAttachmentHistoryService;
import com.tjec.project.system.service.IBmsAttachmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luoliang
 * @date 2018/6/20
 */
@Service

public class FileInfoServiceImpl implements FileInfoService {
    @Resource
    private IBmsAttachmentService iBmsAttachmentService;
    @Resource
    private IBmsAttachmentHistoryService iBmsAttachmentHistoryService;

    @Override
    public String addFileInfo(FileInfo fileInfo) {
        BmsAttachment bmsAttachment = new BmsAttachment();
        bmsAttachment.setDataField(fileInfo.getIdentifier());

        List<BmsAttachment> list = iBmsAttachmentService.selectBmsAttachmentList(bmsAttachment);
        list.sort((a, b) -> Integer.compare(b.getVersion().intValue(), a.getVersion().intValue()));

        //单文件，不考虑文件名与类型
        bmsAttachment.setFileName(fileInfo.getFilename());
        bmsAttachment.setContentType(fileInfo.getType());
        bmsAttachment.setContentLength(fileInfo.getTotalSize());
        //图片存储路径
        bmsAttachment.setStoragePath(fileInfo.getLocation());
        bmsAttachment.setDelFlag(AttachmentConstants.NO_DEL);
        bmsAttachment.setVersion(1L);
        bmsAttachment.setCreateTime(DateUtils.getNowDate());

        //覆盖即原文件删除（逻辑）
        /*if (fileInfo.getCover()) {
            if (!CollectionUtils.isEmpty(list)) {
                BmsAttachment old = list.get(0);
                old.setDelFlag(AttachmentConstants.DEL);
                old.setUpdateTime(DateUtils.getNowDate());
                bmsAttachmentMapper.updateBmsAttachment(old);
            }
        } else {
            //不覆盖即更新，原文件移入历史文件
            if (!CollectionUtils.isEmpty(list)) {
                BmsAttachment old = list.get(0);
                //原本复制一份到历史表
                copyToHistory(old);
                //新文件版本号+1
                bmsAttachment.setVersion(old.getVersion() + 1L);
            }
        }*/
        iBmsAttachmentService.insertBmsAttachment(bmsAttachment);
        //根据返回的主键id，去查询guid
        /*if(null != bmsAttachment.getId()){
            BmsAttachment view = bmsAttachmentMapper.selectBmsAttachmentByKey(bmsAttachment.getId());
            ret = view.getGuid();
        }*/
        //Boolean ret = iBmsAttachmentService.updateBmsAttachment("bf804db5-abad-11ea-b316-0242ac110003", "78c385ba-abb8-11ea-b316-0242ac110003");
        return bmsAttachment.getGuid();
    }

    private void copyToHistory(BmsAttachment old) {
        BmsAttachmentHistory history = new BmsAttachmentHistory();
        BeanUtils.copyProperties(old, history);
        history.setAttachId(old.getGuid());
        iBmsAttachmentHistoryService.insertBmsAttachmentHistory(history);
    }

    @Override
    public void delFileInfo(FileInfo fileInfo) {
        String guid = fileInfo.getId();
        BmsAttachment conditions = new BmsAttachment();
        conditions.setGuid(guid);
        conditions.setDelFlag(AttachmentConstants.NO_DEL);
        List<BmsAttachment> oldList = iBmsAttachmentService.selectBmsAttachmentList(conditions);
        if (!CollectionUtils.isEmpty(oldList)) {
            //原本复制一份到历史表
            copyToHistory(oldList.get(0));
            BmsAttachment bmsAttachment = new BmsAttachment();
            bmsAttachment.setGuid(guid);
            bmsAttachment.setUpdateTime(DateUtils.getNowDate());
            bmsAttachment.setDelFlag(AttachmentConstants.DEL);
            iBmsAttachmentService.updateBmsAttachment(bmsAttachment);
        }

    }
}
